package com.scalar.chiptrack.workflow;

import java.awt.event.MouseEvent;
import java.io.File;

import com.jgraph.JGraph;
import com.jgraph.graph.CellMapper;
import com.jgraph.graph.Edge;
import com.jgraph.graph.EdgeView;
import com.jgraph.graph.GraphModel;
import org.w3c.dom.Document;

// Defines a Graph that uses the Shift-Button (Instead of the Right
// Mouse Button, which is Default) to add/remove point to/from an edge.
public class WorkflowGraph extends JGraph 
{
	protected Editor editor;		
	protected WorkflowGraphUI graphUI = null;
	protected WorkflowManager workflowManager;
		
	// Construct the Graph using the Model as its Data Source
	public WorkflowGraph(GraphModel model, Editor editor) 
	{
		super(model);
		this.editor = editor;
		updateUI();
		
		workflowManager = new WorkflowManager( this );
		
		// Use a Custom Marquee Handler
		setMarqueeHandler(new WorkflowMarqueeHandler( this, editor ) );
		// Tell the Graph to Select new Cells upon Insertion
		setSelectNewCells(true);
		// Make Ports Visible by Default
		setPortsVisible(true);
		// Use the Grid (but don't make it Visible)
		setGridEnabled(true);
		// Set the Grid Size to 10 Pixel
		setGridSize(6);
		// Set the Tolerance to 2 Pixel
		setTolerance(2);
	}
		
	public Editor getEditor()
	{
		return editor;
	}
		
	public WorkflowManager getWorkflowManager()
	{
		return workflowManager;
	}
	
	public void updateUI() 
	{
		setUI(new WorkflowGraphUI());
		invalidate();
	}
	
	// Override Superclass Method to Return Custom EdgeView
	protected EdgeView createEdgeView(Edge e, CellMapper cm) 
	{
		// Return Custom EdgeView
		return new EdgeView(e, this, cm) {
			// Override Superclass Method
			public boolean isAddPointEvent(MouseEvent event) {
				// Points are Added using Shift-Click
				return event.isShiftDown();
			}
			// Override Superclass Method
			public boolean isRemovePointEvent(MouseEvent event) {
				// Points are Removed using Shift-Click
				return event.isShiftDown();
			}
		};
	}
	
	public void createWorkflow( String url, String type )
	{
		workflowManager.createWorkflow( url, type );
	}
	
	public void createWorkflow ( File f )
	{
		workflowManager.createWorkflow(f);
	}
	
	public void createWorkflowFromDoc(Document doc)
	{
		workflowManager.createWorkflowFromDoc( doc );
	}
		
	public String getToolTipText(MouseEvent event)
	{
		return workflowManager.getTooltip(event);
	}
}